package cn.iocoder.yudao.module.ai.service.datasetversion;

import cn.iocoder.yudao.module.ai.controller.admin.datasetreleasefile.vo.DatasetReleaseFileRespVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.datasetreleasefile.DatasetReleaseFileDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.datasetversion.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.datasetversion.DatasetVersionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.datasetversion.DatasetVersionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 数据集发版列 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class DatasetVersionServiceImpl implements DatasetVersionService {

    @Resource
    private DatasetVersionMapper datasetVersionMapper;

    @Override
    public Long createDatasetVersion(DatasetVersionSaveReqVO createReqVO) {
        // 插入
        DatasetVersionDO datasetVersion = BeanUtils.toBean(createReqVO, DatasetVersionDO.class);
        datasetVersionMapper.insert(datasetVersion);
        // 返回
        return datasetVersion.getId();
    }

    @Override
    public void updateDatasetVersion(DatasetVersionSaveReqVO updateReqVO) {
        // 校验存在
        // validateDatasetVersionExists(updateReqVO.getId());
        // 更新
        DatasetVersionDO updateObj = BeanUtils.toBean(updateReqVO, DatasetVersionDO.class);
        datasetVersionMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatasetVersion(Long id) {
        // 校验存在
        validateDatasetVersionExists(id);
        // 删除
        datasetVersionMapper.deleteById(id);
    }

    private void validateDatasetVersionExists(Long id) {
        if (datasetVersionMapper.selectById(id) == null) {
            throw exception(DATASET_VERSION_NOT_EXISTS);
        }
    }

    @Override
    public DatasetVersionDO getDatasetVersion(Long id) {
        return datasetVersionMapper.selectById(id);
    }

    @Override
    public PageResult<DatasetVersionDO> getDatasetVersionPage(DatasetVersionPageReqVO pageReqVO) {
        return datasetVersionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DatasetVersionRespVO> getVersionListByDatasetId(Long datasetId) {
        LambdaQueryWrapper<DatasetVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DatasetVersionDO::getDatasetId, datasetId);
        queryWrapper.orderByDesc(DatasetVersionDO::getSortNum);
        List<DatasetVersionDO> datasetVersionDOS = datasetVersionMapper.selectList(queryWrapper);
        return BeanUtils.toBean(datasetVersionDOS, DatasetVersionRespVO.class);
    }
}